# coding=utf-8


from urllib import request
import re
import time
import random
import os

ua_list=[r'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.6.2.15747',
            r'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
            r'Mozilla/5.0 (iPhone; U; CPU like Mac OS X) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3',
            r'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1']

user_agent = r"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.6.2.15747"
header = {"User-Agent": user_agent}

u_data = open("1","r")
url_list = u_data.readlines()
u_data.close()

reg = r'(https://(?:am\.zd|qna\.sm|a\.zd).*?\_e600.jpg)'
imgre = re.compile(reg)
for page_url in url_list:
    ua = random.choice(ua_list)
    req = request.Request(url=page_url,headers={'User-Agent':ua})
    page = request.urlopen(req).read().decode()
    imglist = re.findall(imgre, str(page))
    total_img = len(imglist)
    for x in range(total_img):
        print(imglist[x])
        imgname='img/'+imglist[x].split('/').pop()
        print(imgname,x+1,'/',total_img)
        if os.path.isfile(imgname) == False:
            req = request.Request(url=imglist[x],headers={'User-Agent':ua})
            imgdata = request.urlopen(req).read()
            fn = open(imgname,'wb')
            fn.write(imgdata)
            fn.close()
            time.sleep(1)